package com.mc.main;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Start point of program, verify the user and move on
 * Added MD5 encrypted passwords (Kenneth)
 *  
 * @Author Kenneth Andersson and Mikael Bertlin
 * 
 */

public class StartMe extends Activity {
	
	private String[] users = {"micke", "olle", "pelle"}; 
	//The MD5 hash stored passwords are "micke","olle","pelle".
	private String[] pwd = {"208157aa1dc70909d4d58907f7dc20eb", "264fcf838c8e4b9d17c510cd5b8b9b78", "5574331fd0ffcb0a268e59f8c5a0cc5d"};
	private String TAG = "StartMe";
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		final Button button = (Button) findViewById(R.id.ButtonLogin);
		button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				boolean uname_ok = false;
				boolean pwd_ok = false;
				String err_string ="";
				final EditText username = (EditText) findViewById(R.id.username);
				final EditText password = (EditText) findViewById(R.id.password);
				String user = username.getText().toString();
				String pass = password.getText().toString();
				int index = 0;
	                
				//Generate a MD5 hash of the typed password
				pass = getMd5Hash(pass);
				
				//Verify User name    	
				for(int i=0; i<users.length; i++){
					Log.v(TAG, "Matching: '" + users[i] + "' to '" + user + "' ok? " + uname_ok);
	                if(user.matches(users[i]) && (user.length() == users[i].length())){
	                	uname_ok = true;
	                	index = i;
	                	break;
	                }
	            }
				
				//Verify Password
	            if(uname_ok){
	            	Log.v(TAG, "Matching: '" + pwd[index] + "' to '" + pass + "' ok? " + pwd_ok);
	                if(pass.matches(pwd[index]) && (pass.length() == pwd[index].length())){
	                	pwd_ok = true;
	                	//Start next activity
	                	Intent myInt = new Intent(StartMe.this, UnitPicker.class);
	                	startActivity(myInt);
	                }
	                		
	            } else { err_string = "Invalid username"; } 
	            if(!pwd_ok && uname_ok)
	            	err_string = "Wrong password";
	            if (!uname_ok || !pwd_ok) //Show the problem
	            	Toast.makeText(StartMe.this, err_string, Toast.LENGTH_SHORT).show();
			}	
		});	
		
		final Button jump = (Button) findViewById(R.id.buttonJumpLogin);
		jump.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				//Start next activity
            	Intent myInt = new Intent(StartMe.this, UnitPicker.class);
            	startActivity(myInt);				
			}		
		});		
		
	}
		
	private static String getMd5Hash(String input) {
	        try {
	            MessageDigest md = MessageDigest.getInstance("MD5");
	            byte[] messageDigest = md.digest(input.getBytes());
	            BigInteger number = new BigInteger(1,messageDigest);

	            String md5 = number.toString(16);
	            while (md5.length() < 32)
	                md5 = "0" + md5;
	            return md5;
	        } catch(NoSuchAlgorithmException e) {
	            Log.e("MD5", e.getMessage());
	            return null;
	        }
	}
}
